<?php
class Datum {
    public static function entree($list, $pk='id', $fid = 'fid', $sun = '_', $root = 0){
        $tree = [];
        if(is_array($list)) {
            $temp = [];
            foreach ($list as $key => $value) {
                $temp[$value[$pk]] =& $list[$key];
            }
            foreach ($list as $key => $data) {
                $_fid =  $data[$fid];
                if($root == $_fid) {
                    $tree[] =& $list[$key];
                }else{
                    if(isset($temp[$_fid])){
                        $f =& $temp[$_fid];
                        $f[$sun][] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }
    public static function detree($tree, $child = '_child', $order='id', &$list = array()){
        if(is_array($tree)) {
            $refer = array();
            foreach ($tree as $key => $value) {
                $temp = $value;
                if(isset($reffer[$child])){
                    unset($reffer[$child]);
                    self::detree($value[$child], $child, $order, $list);
                }
                $list[] = $reffer;
            }
            $list = list_sort_by($list, $order, $sortby='asc');
        }
        return $list;
    }    
}